package com.itheima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.PageResult;
import com.itheima.domain.SmsCoupon;
import com.itheima.domain.SmsCouponProductCategoryRelation;
import com.itheima.domain.SmsCouponProductRelation;
import com.itheima.mapper.SmsCouponMapper;
import com.itheima.mapper.SmsCouponProductCategoryRelationMapper;
import com.itheima.mapper.SmsCouponProductRelationMapper;
import com.itheima.service.SmsCouponProductCategoryRelationService;
import com.itheima.service.SmsCouponService;
import com.itheima.vo.CouponVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 优惠券表 服务实现类
 * </p>
 *
 * @author zhuguo
 * @since 2022-11-10
 */
@DubboService
public class SmsCouponServiceImpl extends ServiceImpl<SmsCouponMapper, SmsCoupon> implements SmsCouponService {

    @Autowired
    SmsCouponMapper smsCouponMapper;
    @Autowired
    SmsCouponProductCategoryRelationMapper couponProductCategoryRelationMapper;
    @Autowired
    SmsCouponProductRelationMapper couponProductRelationMapper;
    @Override
    public PageResult pageByNameAndType(String name, Integer pageNum, Integer pageSize, Integer type) {
        Page<SmsCoupon> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsCoupon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(name),SmsCoupon::getName,name).eq(type!=null,SmsCoupon::getType,type);
        smsCouponMapper.selectPage(page,queryWrapper);
        return new PageResult(pageNum,pageSize,page.getTotal(),page.getPages(),page.getRecords());

    }

    @Override
    public CouponVo getInfoById(Integer id) {
        SmsCoupon coupon = smsCouponMapper.selectById(id);
        CouponVo couponVo = new CouponVo();
        BeanUtil.copyProperties(coupon,couponVo);
        //根据优惠卷绑定的商品分类查询
        LambdaQueryWrapper<SmsCouponProductCategoryRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmsCouponProductCategoryRelation::getCouponId,coupon.getId());
        List<SmsCouponProductCategoryRelation> categoryRelations = couponProductCategoryRelationMapper.selectList(queryWrapper);
        couponVo.setProductCategoryRelationList(categoryRelations);
        //根据优惠卷id查询绑定的商品信息
        LambdaQueryWrapper<SmsCouponProductRelation> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(SmsCouponProductRelation::getCouponId,coupon.getId());
        List<SmsCouponProductRelation> couponProductRelations = couponProductRelationMapper.selectList(queryWrapper1);
        couponVo.setProductRelations(couponProductRelations);

        return couponVo;

    }

    @Override
    public void deleteById(Integer id) {
        //先删除优惠券表中信息
        smsCouponMapper.deleteById(id);
        //删除优惠券绑定的品牌分类信息
        LambdaQueryWrapper<SmsCouponProductCategoryRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmsCouponProductCategoryRelation::getCouponId,id);
        couponProductCategoryRelationMapper.delete(queryWrapper);
        //删除优惠卷绑定的商品信息
        LambdaQueryWrapper<SmsCouponProductRelation> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(SmsCouponProductRelation::getCouponId,id);
        couponProductRelationMapper.delete(queryWrapper1);


    }
}
